﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class TMCO01
    {
        #region ==AUF===========
        public static readonly DependencyProperty AUFProperty = DependencyProperty.Register("AUF",
            typeof(string), typeof(TMCO01),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAUFChanged));
        public string AUF
        {
            get
            {
                return (string)GetValue(AUFProperty);
            }
            set
            {
                SetValue(AUFProperty, value);
            }
        }
        private static void OnAUFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TMCO01;
            if (src != null)
            {
                src.AUF = (string)e.NewValue;
                src.Load();
            }
        }
        #endregion 
      
        public TMCO01()
        {
            MV = new VICC_ABAUFM(); 
            InitializeComponent(); 
        }
        string LastAUF = "";
        public void Load()
        {
            if (LastAUF == AUF)
                return;
            LastAUF = AUF;
            DataRow dwData = MV.SetEnv.Tables[TB].NewRow();
            string ordertype = Helpers.Helper.Left(AUF, 1);
            dwData["ABAVKAUFTYP"] = Helpers.Helper.Left(AUF, 1);
            dwData["ABAVKAUFKRS"] = AUF.Substring(1, 2);
            dwData["ABAVKAUFLFD"] = AUF.Substring(3, 6);
            dwData["ABAVKAUFPOS"] = Helpers.Helper.Right(AUF, 3); 
            DataSet SResult = ((VICC_ABAUFM)MV).QueryICC_ABAUFM(dwData, 3);
            
            if (SResult.Tables[0].Rows.Count == 0)
            {
                ((VICC_ABAUFM)MV).LoadMU(AUF);
            }
            MergeData(SResult);
        }
         
        private void Refresh_Click(object sender, RoutedEventArgs e)
        {
            ((VICC_ABAUFM)MV).LoadMU(AUF);
        }
    }
}
